import cgi
import uuid
import datetime
from google.appengine.ext import db
from django.utils import simplejson as json
import MessagesUtil as mu

#thread = mu.Thread()
#thread.author = "al"
#thread.group = "pod1"
#thread.put()

  
#message = mu.Message()
#message.threadkey = str(thread.key())
#message.subject = "First Thread"
#message.author = "al"
#message.group = "pod1"
#message.message = "This is my first message"
#message.put()


#msgs = db.GqlQuery("SELECT * FROM Message")
#for m in msgs:
#  print "thread key " + m.threadkey#

#thread = mu.Thread.get(m.threadkey)

#print "thread subject " + thread.subject

import cgi
form = cgi.FieldStorage()

print "Content-type: text/html\n\n"

if ("request" in form):
  if (form["request"].value == "GetGroups"):
    # this is just mock data for now
    groups = [ mu.Group(name="Cat talk", threads=0).toObject() , 
               mu.Group(name="Dog discussion", threads=0).toObject() , 
               mu.Group(name="Lizard Learning", threads=0).toObject()  ]
    print json.dumps(groups)

  if (form["request"].value == "GetMessages"):
    # this is just mock data for now
    threads = [ mu.Thread(subject="thread a", author='Al').toObject() , 
               mu.Thread(subject="thread b", author='Elizabeth').toObject() , 
               mu.Thread(subject="thread c", author='Connor').toObject() ,
               mu.Thread(subject="thread d", author='Kate').toObject()   ]
    print json.dumps(threads)